package com.voute.View.Activity;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.voute.App.App;
import com.voute.Base.BaseActivity;
import com.voute.Model.Bean.BaseBean;
import com.voute.Presenter.IResetPasswordPresenter;
import com.voute.Presenter.Impl.ResetPasswordPresenterImpl;
import com.voute.R;
import com.voute.View.IView.IForgetPasswordView;

/**
 * 忘记密码画面
 * Created by liucxu on 2017/2/14.
 */

public class ForgetPasswordActivity extends BaseActivity implements IForgetPasswordView {

    private RelativeLayout back_layout;
    private RelativeLayout register_first_step;
    private EditText register_phone_et;
    private TextView register_next_step;
    private TextView user_registered_tip;
    private TextView tv_title;

    private RelativeLayout register_second_step;
    private EditText register_verification_et;
    private TextView register_count_et;
    private EditText register_user_password_et;
    private TextView register;
    private CountDownTimer timer;
    private MaterialDialog progressDialog;
    private IResetPasswordPresenter presenter;

    private int current_page = 1;


    @Override
    public int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void setupView() {
        back_layout = $(R.id.back_layout);
        register_first_step = $(R.id.register_first_step);
        register_phone_et = $(R.id.register_phone_et);
        register_next_step = $(R.id.register_next_step);
        user_registered_tip = $(R.id.user_registered_tip);
        tv_title = $(R.id.tv_title);

        register_second_step = $(R.id.register_second_step);
        register_verification_et = $(R.id.register_verification_et);
        register_count_et = $(R.id.register_count_et);
        register_user_password_et = $(R.id.register_user_password_et);
        register = $(R.id.register);
        register.setText("提交");
        tv_title.setText("重置密码");
        back_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                App.GetInstance().finishActivity(ForgetPasswordActivity.this);
            }
        });

        presenter = new ResetPasswordPresenterImpl(this);

        register_next_step.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkValidate()) {
                    presenter.getSMSCode(register_phone_et.getText().toString());
                    if (timer != null) {
                        timer.cancel();
                    }
                    timer = new CountDownTimer(60000, 1000) {
                        @Override
                        public void onTick(long l) {
                            register_count_et.setText((l / 1000) + "秒后重发");
                        }

                        @Override
                        public void onFinish() {
                            register_count_et.setEnabled(true);
                            register_count_et.setText("重新获取验证码");
                        }
                    };
                    timer.start();
                }
            }
        });


        register_count_et.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (timer != null) {
                    timer.cancel();
                }
                timer = new CountDownTimer(60000, 1000) {
                    @Override
                    public void onTick(long l) {
                        register_count_et.setText((l / 1000) + "秒后重发");
                    }

                    @Override
                    public void onFinish() {
                        register_count_et.setEnabled(true);
                        register_count_et.setText("重新获取验证码");
                    }
                };
                timer.start();
                presenter.getSMSCode(register_phone_et.getText().toString());
            }
        });


        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (checkValidate()) {
                    presenter.resetPassword(register_verification_et.getText().toString(), register_user_password_et.getText().toString(), register_phone_et.getText().toString());
                }
            }
        });
    }

    @Override
    public void setupData() {

    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new MaterialDialog.Builder(this)
                    .progress(true, 0)
                    .title("提示")
                    .content("请等待...")
                    .autoDismiss(false)
                    .cancelable(true)
                    .build();
        }
        progressDialog.show();
    }

    @Override
    public void hideLoading() {
        progressDialog.dismiss();
    }

    @Override
    public void showError(String errMsg) {
//        Snackbar.make(register_next_step, errMsg, Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(ForgetPasswordActivity.this).title("提示").positiveText("确认").cancelable(false).content(errMsg).build();
        notice_dialog.show();
    }

    @Override
    public void setRightVeritificationCode() {
        if (current_page == 1) {
            register_first_step.setVisibility(View.GONE);
            register_second_step.setVisibility(View.VISIBLE);
            register_count_et.setEnabled(false);
            current_page = 2;
            if (timer != null) {
                timer.cancel();
            }
            timer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long l) {
                    register_count_et.setText((l / 1000) + "秒后重发");
                }

                @Override
                public void onFinish() {
                    register_count_et.setEnabled(true);
                    register_count_et.setText("重新获取验证码");
                }
            };
            timer.start();
        } else {
            register_count_et.setEnabled(false);
            if (timer != null) {
                timer.cancel();
            }
            timer = new CountDownTimer(60000, 1000) {
                @Override
                public void onTick(long l) {
                    register_count_et.setText(((int) (l / 1000)) + "秒后重发");
                }

                @Override
                public void onFinish() {
                    register_count_et.setEnabled(true);
                    register_count_et.setText("重新获取验证码");
                }
            };
            timer.start();
        }
    }

    @Override
    public void setErrorVeritificationCode(BaseBean response) {
        register_count_et.setText("重新获取验证码");
        register_count_et.setEnabled(true);
//        Snackbar.make(register_count_et, response.getMsg(), Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public boolean checkValidate() {
        if (current_page == 1) {
            if (TextUtils.isEmpty(register_phone_et.getText().toString())) {
                showError("请输入手机号码");
                return false;
            }
        } else if (current_page == 2) {
            if (TextUtils.isEmpty(register_verification_et.getText().toString())) {
                showError("请输入验证码");
                return false;
            }
            if (TextUtils.isEmpty(register_user_password_et.getText().toString())) {
                showError("请输入新密码");
                return false;
            }
            return true;
        }
        return true;
    }

    @Override
    public void doResetPasswordSuccess() {
        App.GetInstance().finishActivity(this);
    }

    @Override
    public void doResetPasswordFailure(BaseBean response) {
//        Snackbar.make(register_count_et, response.getMsg(), Snackbar.LENGTH_SHORT).show();
        MaterialDialog notice_dialog = new MaterialDialog.Builder(ForgetPasswordActivity.this).title("提示").positiveText("确认").cancelable(false).content(response.getMsg()).build();
        notice_dialog.show();
    }
}
